﻿using HalconDotNet;
using NeuroCheck.PlugInLib.API;
using NeuroCheck.PlugInLib.Base;
using NeuroCheck.PlugInLib.ImageProcessing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CLVisionHalconNC620.Transform
{
    public class RegionTransform
    {
        public static void HalconRegion2NCRegion(NcPICheckFunctionBase ncPICheckFunctionBase, HRegion region, int TrayIndex)
        {
            HTuple numberObj;
            HOperatorSet.CountObj(region, out numberObj);

            if (numberObj > 1)
            {
                NcPIUserException NcException = new NcPIUserException("对象数量不能多于1个");
                throw NcException;
            }
            else if (numberObj <= 0)
            {
                NcPIUserException NcException = new NcPIUserException("对象为空");
            }
            HTuple row1, column1, row2, column2;
            region.SmallestRectangle1(out row1, out column1, out row2, out column2);

            int width, height;
            width = row2 - row1;
            height = column2 - column1;

            NcPIROICollection colROIsOutput = new NcPIROICollection();
            NcPIAOI aoiROI = new NcPIAOI(column1, row1, height, width, 0);
            colROIsOutput.Add(aoiROI);
            NcPIDataTray.AddROICollection(colROIsOutput, TrayIndex, ncPICheckFunctionBase);
        }
        public static NcPIROICollection HalconRegion2NCRegion( HRegion region)
        {
            HTuple numberObj;
            HOperatorSet.CountObj(region, out numberObj);

            if (numberObj > 1)
            {
                NcPIUserException NcException = new NcPIUserException("对象数量不能多于1个");
                throw NcException;
            }
            else if (numberObj <= 0)
            {
                NcPIUserException NcException = new NcPIUserException("对象为空");
            }
            HTuple row1, column1, row2, column2;
            region.SmallestRectangle1(out row1, out column1, out row2, out column2);

            int width, height;
            width = row2 - row1;
            height = column2 - column1;

            NcPIROICollection colROIsOutput = new NcPIROICollection();
            NcPIAOI aoiROI = new NcPIAOI(column1, row1, height, width, 0);
            colROIsOutput.Add(aoiROI);
            return colROIsOutput;
            
        }

        public static HObject NCRegion2HalconRegion(ICollection<NcPIROIBase> NcRoi,out int ErrorCode,out string ErrorText)
        {
            try
            {
                ErrorCode = 0;
                ErrorText = "";
                HObject HalconRoi, TempRoi = null;
                HOperatorSet.GenEmptyObj(out HalconRoi);
                foreach (NcPIROIBase temp in NcRoi)
                {
                    Type RoiType = temp.GetType();
                    switch (RoiType.FullName)
                    {
                        case "NeuroCheck.PlugInLib.ImageProcessing.NcPIAOI":
                            HOperatorSet.GenRectangle1(out TempRoi, temp.AOIOrigin.Y, temp.AOIOrigin.X, temp.AOIOrigin.Y + temp.AOIHeight, temp.AOIOrigin.X + temp.AOIWidth);
                            HOperatorSet.ConcatObj(HalconRoi, TempRoi, out HalconRoi);
                            break;
                        case "NeuroCheck.PlugInLib.ImageProcessing.NcPICircle":
                            HOperatorSet.GenCircle(out TempRoi, temp.AOICenter.Y, temp.AOICenter.X, ((NcPICircle)temp).Radius);
                            HOperatorSet.ConcatObj(HalconRoi, TempRoi, out HalconRoi);
                            break;
                        case "NeuroCheck.PlugInLib.ImageProcessing.NcPIPolygon":
                            Point[] pt = ((NcPIPolygon)temp).GetPoints();
                            HTuple columns = new HTuple();
                            HTuple rows = new HTuple();

                            //获取多边形的点
                            for (int i = 0; i < pt.Length; i++)
                            {
                                columns[i] = pt[i].X + ((NcPIPolygon)temp).AOIOrigin.X;
                                rows[i] = pt[i].Y + ((NcPIPolygon)temp).AOIOrigin.Y;
                            }
                            HOperatorSet.GenRegionPolygonFilled(out TempRoi, rows, columns);
                            HOperatorSet.ConcatObj(HalconRoi, TempRoi, out HalconRoi);
                            break;
                        case "NeuroCheck.PlugInLib.ImageProcessing.NcPILine":
                            HTuple beginRow = new HTuple();
                            HTuple beginColumn = new HTuple();
                            HTuple endRow = new HTuple();
                            HTuple endColumn = new HTuple();

                            //获取线的起点和终点
                            beginRow = ((NcPILine)temp).StartPoint.Y;
                            beginColumn = ((NcPILine)temp).StartPoint.X;
                            endRow = ((NcPILine)temp).EndPoint.Y;
                            endColumn = ((NcPILine)temp).EndPoint.X;

                            HOperatorSet.GenRegionLine(out TempRoi, beginRow, beginColumn, endRow, endColumn);
                            HOperatorSet.ConcatObj(HalconRoi, TempRoi, out HalconRoi);
                            break;
                        default:
                            if (temp.Region != null)
                            {

                            }
                            else
                            {
                                HOperatorSet.GenRectangle1(out TempRoi, temp.AOIOrigin.Y, temp.AOIOrigin.X, temp.AOIOrigin.Y + temp.AOIHeight, temp.AOIOrigin.X + temp.AOIWidth);
                                HOperatorSet.ConcatObj(HalconRoi, TempRoi, out HalconRoi);
                            }
                            break;
                    }
                }
                return HalconRoi;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = ex.Message;
                return null;
            }
         
        }
    }
}
